﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;

namespace BLL
{
    public class ShareBLL:IShareBLL
    {
        //传入用户id、图片id和shareDescription设置分享
        public bool sharePhoto(int userId, int photoId, string shareDescription)
        {
            bool validator = false;
            DAL.ShareDAL shareDal = (DAL.ShareDAL)DALFAC.DataAccess.CreateShare();
            if (null != shareDal)
            {
                Model.Share shareModel = new Model.Share();
                shareModel.setSH_userId(userId);
                shareModel.setSH_photoId(photoId);
                shareModel.setSH_time(DateTime.Now);
                shareModel.setSH_description(shareDescription);
                validator = shareDal.Addshare(shareModel);
            }

            return validator;
        }


        //传入图片id获取是否存在分享
        public bool shareExist(int shareId)
        {
            bool validator = false;
            DAL.ShareDAL shareDal = (DAL.ShareDAL)DALFAC.DataAccess.CreateShare();
            if (null != shareDal)
            {
                validator = shareDal.shareExistByShareId(shareId);
            }

            return validator;
        }


        //删除给定分享
        public bool deleteShare(int shareId)
        {
            bool validator = false;
            DAL.ShareDAL shareDal = (DAL.ShareDAL)DALFAC.DataAccess.CreateShare();
            if (null != shareDal)
            {
                validator = shareDal.deleteShareByShareId(shareId);              
            }

            return validator;
        }


        //删除所有分享
        public bool deleteAllShare(int photoId)
        {
            bool validator = false;
            DAL.ShareDAL shareDal = (DAL.ShareDAL)DALFAC.DataAccess.CreateShare();
            if (null != shareDal)
            {
                validator = shareDal.deleteShareByPhotoId(photoId);
            }

            return validator;
        }


        //获取所有分享
        public List<Model.Share> getAllShareByUserID(int userId)
        {
            List<Model.Share> list = new List<Model.Share>();
            DAL.ShareDAL shareDal = (DAL.ShareDAL)DALFAC.DataAccess.CreateShare();
            if (null != shareDal)
            {
                list = shareDal.getModelByUserId(userId);
            }

            return list;
        }


        //获取所有分享
        public List<Model.Share> getAllShareByPhotoId(int photoId)
        {
            List<Model.Share> list = new List<Model.Share>();
            DAL.ShareDAL shareDal = (DAL.ShareDAL)DALFAC.DataAccess.CreateShare();
            if (null != shareDal)
            {
                list = shareDal.getModelByPhotoId(photoId);
            }

            return list;
        }


        //获取所有分享
        public List<Model.Share> getFollowShare(int userId)
        {
            List<Model.Share> shareList = new List<Model.Share>();
            List<Model.Follows> folloLlist = new List<Model.Follows>();
            DAL.ShareDAL shareDal = (DAL.ShareDAL)DALFAC.DataAccess.CreateShare();
            DAL.FollowsDAL followDal = (DAL.FollowsDAL)DALFAC.DataAccess.CreateFollows();
            if (null != followDal && null != shareDal)
            {
                folloLlist = followDal.GetMyFollows(userId);
                for(int i=0;i < folloLlist.Count;i++)
                {
                    int userIdTo = folloLlist[i].getFL_IdTo();
                    List<Model.Share> tempList = shareDal.getModelByUserId(userIdTo);

                    for (int j = 0; j < tempList.Count; j++) 
                    {
                        if (tempList[j].getSH_time().AddDays(2).CompareTo(DateTime.Now) > 0)
                        {
                            Model.Share model = tempList[j];
                            shareList.Add(model);
                        }
                    }

                    tempList.Clear();
                } 
            }

            return shareList;
        }
    }
}
